/*     */ package com.newcrm.manager;
/*     */ 
/*     */ import com.newcrm.db.ConnectionManager;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.util.Util;
/*     */ import java.io.IOException;
/*     */ import java.nio.file.Paths;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.apache.lucene.analysis.Analyzer;
/*     */ import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
/*     */ import org.apache.lucene.document.Document;
/*     */ import org.apache.lucene.document.Field;
/*     */ import org.apache.lucene.document.IntField;
/*     */ import org.apache.lucene.document.StringField;
/*     */ import org.apache.lucene.document.TextField;
/*     */ import org.apache.lucene.index.DirectoryReader;
/*     */ import org.apache.lucene.index.IndexReader;
/*     */ import org.apache.lucene.index.IndexWriter;
/*     */ import org.apache.lucene.index.IndexWriterConfig;
/*     */ import org.apache.lucene.index.IndexWriterConfig.OpenMode;
/*     */ import org.apache.lucene.index.Term;
/*     */ import org.apache.lucene.search.BooleanClause;
/*     */ import org.apache.lucene.search.BooleanQuery;
/*     */ import org.apache.lucene.search.IndexSearcher;
/*     */ import org.apache.lucene.search.Query;
/*     */ import org.apache.lucene.search.ScoreDoc;
/*     */ import org.apache.lucene.search.TermQuery;
/*     */ import org.apache.lucene.search.TopDocs;
/*     */ import org.apache.lucene.store.Directory;
/*     */ import org.apache.lucene.store.FSDirectory;
/*     */ import org.apache.lucene.util.QueryBuilder;
/*     */ 
/*     */ public class LuceneManager
/*     */ {
/*  52 */   private static Logger logger = Logger.getLogger(LuceneManager.class
/*  53 */     .getName());
/*     */ 
/*  55 */   private static LuceneManager manager = null;
/*  56 */   private boolean isRunning = false;
/*  57 */   private Directory dir = null;
/*  58 */   private IndexSearcher searcher = null;
/*  59 */   private Analyzer analyzer = null;
/*     */ 
/*  62 */   private int totalNum = 0;
/*  63 */   private int indexNum = 0;
/*     */ 
/*     */   private LuceneManager(String indexPath)
/*     */   {
/*  74 */     this.isRunning = false;
/*     */     try {
/*  76 */       this.dir = FSDirectory.open(Paths.get(indexPath, new String[0]));
/*  77 */       this.analyzer = new SmartChineseAnalyzer();
/*     */     } catch (IOException e) {
/*  79 */       logger.warn("open path failed", e);
/*     */     }
/*     */   }
/*     */ 
/*     */   public static synchronized LuceneManager getInstance(String indexPath) {
/*  84 */     if (manager == null) {
/*  85 */       manager = new LuceneManager(indexPath);
/*     */     }
/*  87 */     return manager;
/*     */   }
/*     */ 
/*     */   public synchronized void reBuildIndex()
/*     */   {
/*  94 */     if (this.isRunning) {
/*  95 */       return;
/*     */     }
/*     */ 
/*  98 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  99 */     if (conn == null) {
/* 100 */       return;
/*     */     }
/*     */ 
/* 103 */     this.totalNum = 0;
/* 104 */     this.indexNum = 0;
/*     */     try
/*     */     {
/* 107 */       indexCustomers(conn, true);
/*     */     } finally {
/* 109 */       this.isRunning = false;
/*     */       try {
/* 111 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 113 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public int getTotalNum()
/*     */   {
/* 125 */     return this.totalNum;
/*     */   }
/*     */ 
/*     */   public int getIndexNum()
/*     */   {
/* 134 */     return this.indexNum;
/*     */   }
/*     */ 
/*     */   public boolean getRunningStatus()
/*     */   {
/* 143 */     return this.isRunning;
/*     */   }
/*     */ 
/*     */   public synchronized void indexCustomers(Connection conn, boolean isRebuild)
/*     */   {
/* 152 */     if (this.dir == null) {
/* 153 */       logger.warn("dir or iwc is null");
/* 154 */       return;
/*     */     }
/* 156 */     if (this.isRunning) {
/* 157 */       return;
/*     */     }
/*     */ 
/* 160 */     if (conn == null) {
/* 161 */       return;
/*     */     }
/*     */ 
/* 164 */     this.isRunning = true;
/* 165 */     CustomerManager manager = new CustomerManager();
/* 166 */     IndexWriter writer = null;
/*     */     try {
/* 168 */       IndexWriterConfig iwc = new IndexWriterConfig(this.analyzer);
/*     */ 
/* 170 */       if (isRebuild)
/* 171 */         iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
/*     */       else {
/* 173 */         iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
/*     */       }
/*     */ 
/* 176 */       writer = new IndexWriter(this.dir, iwc);
/*     */ 
/* 179 */       if (isRebuild) {
/* 180 */         this.totalNum = manager.clearIndexFlag(conn);
/*     */       } else {
/* 182 */         this.totalNum = 0;
/* 183 */         this.indexNum = 0;
/*     */       }
/*     */ 
/* 186 */       List items = manager
/* 187 */         .getNeedIndexCustomers(conn);
/*     */       do
/*     */       {
/* 191 */         if (isRebuild) {
/* 192 */           this.indexNum += items.size();
/*     */         }
/*     */ 
/* 196 */         StringBuffer ids = new StringBuffer();
/*     */ 
/* 198 */         StringBuffer indexIds = new StringBuffer();
/*     */         Integer customerId;
/* 200 */          for (Object itemO : items) {
                Map item = (Map)itemO;
/* 203 */           customerId = (Integer)item.get("CustomerId");
/*     */ 
/* 205 */           if (ids.length() > 0) {
/* 206 */             ids.append(",");
/*     */           }
/* 208 */           ids.append(customerId);
/*     */ 
/* 211 */           Integer deleteFlag = (Integer)item.get("DeleteFlag");
/* 212 */           if ((deleteFlag != null) && (deleteFlag.intValue() == 1))
/*     */           {
/* 214 */             writer.deleteDocuments(new Term[] { new Term(
/* 215 */               "customerId", 
/* 216 */               String.valueOf(customerId)) });
/*     */           }
/*     */           else
/*     */           {
/* 221 */             if (indexIds.length() > 0) {
/* 222 */               indexIds.append(",");
/*     */             }
/* 224 */             indexIds.append(customerId);
/*     */           }
/*     */         }
/*     */ 
/* 228 */         if (indexIds.length() > 0)
/*     */         {
/* 230 */           List extInfos = manager
/* 231 */             .getNeedIndexCustomersExtInfo(conn, 
/* 232 */             indexIds.toString());
/*     */ 
/* 234 */           if ((extInfos != null) && (extInfos.size() > 0)) {
/* 235 */             for (Object extInfoO : extInfos) {
                        Map extInfo = (Map)extInfoO;
/* 236 */               Integer id = (Integer)extInfo.get("CustomerId");
/* 237 */               String value = (String)extInfo.get("Info1");
/* 238 */               setExtInfo(items, id, value);
/*     */             }
/*     */ 
/*     */           }
/*     */ 
/* 243 */           for (Object itemO : items) {
                    Map item = (Map)itemO;
/*     */           {
/* 245 */             customerId = (Integer)item.get("CustomerId");
/*     */ 
/* 247 */             String name = (String)item.get("Name");
/*     */ 
/* 249 */             Integer type = (Integer)item.get("Type");
/*     */ 
/* 251 */             String title = (String)item.get("Title");
/*     */ 
/* 253 */             Integer visible = (Integer)item.get("Visible");
/*     */ 
/* 255 */             String permissionsId = 
/* 256 */               (String)item
/* 256 */               .get("PermissionsId");
/*     */ 
/* 258 */             Document doc = new Document();
/* 259 */             doc.add(new StringField("customerId", 
/* 260 */               String.valueOf(customerId), Field.Store.YES));
/* 261 */             doc.add(new TextField("name", name, 
/* 262 */               Field.Store.YES));
/* 263 */             doc.add(new IntField("type", type.intValue(), 
/* 264 */               Field.Store.YES));
/*     */ 
/* 266 */             if ((title != null) && (title.trim().length() > 0)) {
/* 267 */               doc.add(new TextField("title", 
/* 268 */                 title, Field.Store.NO));
/*     */             }
/* 270 */             doc.add(new StringField("visible", 
/* 271 */               String.valueOf(visible), Field.Store.YES));
/*     */ 
/* 273 */             Object obj = item.get("ExtInfos");
/*     */ 
/* 275 */             if (obj != null)
/*     */             {
/* 277 */               doc.add(new TextField("info", 
/* 278 */                 (String)obj, Field.Store.NO));
/*     */             }
/*     */ 
/* 282 */             if ((visible.intValue() == 4) && 
/* 283 */               (permissionsId != null) && 
/* 284 */               (permissionsId.trim().length() > 0)) {
/* 285 */               doc.add(new StringField(
/* 286 */                 "permissionsId", 
/* 287 */                 permissionsId, Field.Store.YES));
/*     */             }
/*     */ 
/* 291 */             writer.updateDocument(
/* 292 */               new Term("customerId", 
/* 293 */               String.valueOf(customerId)), doc);
/*     */           }
/*     */ 
/*     */         }
/*     */ 
/* 298 */         manager.updateIndexFlag(conn, ids.toString());
/*     */ 
/* 301 */         items = manager.getNeedIndexCustomers(conn);
/*     */ 
/* 189 */         if (items == null) break;  } }while (items.size() > 0);
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 304 */       logger.warn("lucene failed", e);
/*     */     } finally {
/* 306 */       if (writer != null) {
/*     */         try {
/* 308 */           writer.commit();
/* 309 */           writer.close();
/*     */         } catch (IOException e) {
/* 311 */           logger.warn("lucene writer close failed", e);
/*     */         }
/*     */       }
/*     */ 
/* 315 */       this.isRunning = false;
/* 316 */       this.searcher = null;
/*     */     }
/*     */   }
/*     */ 
/*     */   private BooleanQuery getSearchKeyBooleanQuery(String key)
/*     */   {
/* 326 */     BooleanQuery.Builder builder = new BooleanQuery.Builder();
/* 327 */     QueryBuilder queryBuilder = new QueryBuilder(this.analyzer);
/*     */ 
/* 329 */     Query nameQuery = queryBuilder.createPhraseQuery(
/* 330 */       "name", key);
/* 331 */     nameQuery.setBoost(0.7F);
/* 332 */     builder.add(nameQuery, BooleanClause.Occur.SHOULD);
/*     */ 
/* 335 */     Query extInfoQuery = queryBuilder.createPhraseQuery(
/* 336 */       "info", key);
/* 337 */     extInfoQuery.setBoost(0.2F);
/* 338 */     builder.add(extInfoQuery, BooleanClause.Occur.SHOULD);
/*     */ 
/* 341 */     Query titleQuery = queryBuilder.createPhraseQuery(
/* 342 */       "title", key);
/* 343 */     titleQuery.setBoost(0.1F);
/* 344 */     builder.add(titleQuery, BooleanClause.Occur.SHOULD);
/*     */ 
/* 346 */     return builder.build();
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> search(String key, String permissionsId)
/*     */   {
/* 356 */     List items = new ArrayList();
/*     */ 
/* 358 */     if ((key == null) || (key.trim().length() == 0)) {
/* 359 */       return items;
/*     */     }
/*     */ 
/* 362 */     if (this.searcher == null) {
/*     */       try {
/* 364 */         IndexReader reader = DirectoryReader.open(this.dir);
/* 365 */         this.searcher = new IndexSearcher(reader);
/*     */       } catch (IOException e) {
/* 367 */         logger.warn("create indexSercher failed", e);
/*     */       }
/*     */ 
/* 370 */       if (this.searcher == null) {
/* 371 */         return items;
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 382 */     BooleanQuery query = null;
/* 383 */     if ((permissionsId != null) && (permissionsId.trim().length() > 0))
/*     */     {
/* 386 */       BooleanQuery.Builder permissionsBuilder = new BooleanQuery.Builder();
/*     */ 
/* 388 */       permissionsBuilder.add(
/* 389 */         new TermQuery(new Term("visible", 
/* 390 */         String.valueOf(1))), BooleanClause.Occur.SHOULD);
/*     */ 
/* 393 */       BooleanQuery.Builder builder = new BooleanQuery.Builder();
/*     */ 
/* 395 */       Query visibleQuery = new TermQuery(new Term(
/* 396 */         "visible", 
/* 397 */         String.valueOf(4)));
/* 398 */       builder.add(visibleQuery, BooleanClause.Occur.MUST);
/*     */ 
/* 400 */       Query userIdQuery = new TermQuery(new Term(
/* 401 */         "permissionsId", permissionsId));
/* 402 */       builder.add(userIdQuery, BooleanClause.Occur.MUST);
/* 403 */       permissionsBuilder.add(builder.build(), BooleanClause.Occur.SHOULD);
/*     */ 
/* 406 */       BooleanQuery.Builder allBuilder = new BooleanQuery.Builder();
/*     */ 
/* 408 */       allBuilder.add(getSearchKeyBooleanQuery(key), BooleanClause.Occur.MUST);
/* 409 */       allBuilder.add(permissionsBuilder.build(), BooleanClause.Occur.MUST);
/*     */ 
/* 411 */       query = allBuilder.build();
/*     */     } else {
/* 413 */       query = getSearchKeyBooleanQuery(key);
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/* 418 */       TopDocs results = this.searcher.search(query, 10);
/* 419 */       ScoreDoc[] hits = results.scoreDocs;
/*     */ 
/* 421 */       int numTotalHits = results.totalHits;
/*     */ 
/* 423 */       int end = Math.min(numTotalHits, 10);
/*     */ 
/* 425 */       for (int i = 0; i < end; i++) {
/* 426 */         Document doc = this.searcher.doc(hits[i].doc);
/*     */ 
/* 428 */         int id = Util.str2Int(doc.get("customerId"));
/* 429 */         if (id > 0)
/*     */         {
/* 433 */           int type = Util.str2Int(doc.get("type"));
/*     */ 
/* 435 */           String name = doc.get("name");
/*     */ 
/* 437 */           Map item = new HashMap();
/* 438 */           item.put("id", Integer.valueOf(id));
/* 439 */           item.put("label", name);
/* 440 */           item.put("Type", Integer.valueOf(type));
/*     */ 
/* 442 */           String userId = "";
/*     */ 
/* 444 */           int visible = Util.str2Int(doc.get("visible"));
/* 445 */           if (visible == 4) {
/* 446 */             userId = doc.get("permissionsId");
/*     */           }
/* 448 */           item.put("ShowUserId", userId);
/* 449 */           items.add(item);
/*     */         }
/*     */       }
/*     */     } catch (BooleanQuery.TooManyClauses e) { logger.warn("search keyword faild :" + key, e);
/*     */     } catch (IOException e) {
/* 454 */       logger.warn("search keyword faild :" + key, e);
/*     */     }
/*     */ 
/* 457 */     return items;
/*     */   }
/*     */ 
/*     */   private void setExtInfo(List<Map<String, Object>> items, Integer id, String value)
/*     */   {
/* 469 */     if ((items == null) || (items.size() == 0)) {
/* 470 */       return;
/*     */     }
/* 472 */     if ((id == null) || (id.intValue() <= 0)) {
/* 473 */       return;
/*     */     }
/*     */ 
/* 476 */     if ((value == null) || (value.trim().length() == 0)) {
/* 477 */       return;
/*     */     }
/*     */ 
/* 480 */     for (Map item : items) {
/* 481 */       Integer id2 = (Integer)item.get("CustomerId");
/* 482 */       if (id2 != null)
/*     */       {
/* 486 */         if (id2.intValue() == id.intValue()) {
/* 487 */           Object obj = item.get("ExtInfos");
/* 488 */           if (obj == null) {
/* 489 */             item.put("ExtInfos", value);
/* 490 */             return;
/*     */           }
/*     */ 
/* 493 */           String extInfos = (String)obj;
/* 494 */           if (extInfos.trim().length() == 0) {
/* 495 */             item.put("ExtInfos", value);
/* 496 */             return;
/*     */           }
/*     */ 
/* 499 */           item.put("ExtInfos", extInfos + " " + value);
/* 500 */           return;
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.manager.LuceneManager
 * JD-Core Version:    0.6.1
 */